package com.fxnotes.book;

import com.fxnotes.AppController;
import com.fxnotes.command.OpenBook;
import com.fxnotes.command.ShowBooks;
import com.fxnotes.Resources;
import com.fxnotes.book.presenter.BookDetailsPresenter;
import com.fxnotes.book.presenter.BooksPresenter;
import com.fxnotes.common.ui.ModuleController;
import com.fxnotes.common.ui.NavigationPathBuilder;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;

public class BookModule extends ModuleController {

    private AppController appController;
    private BooksPresenter bookPresenter;
    private BookDetailsPresenter bookDetails;

    @Inject
    public BookModule(EventBus eventBus, BookDetailsPresenter bookDetails, BooksPresenter bookPresenter) {
        super(Resources.MODULE_BOOKS);
        this.bookPresenter = bookPresenter;
        this.bookDetails = bookDetails;

        addPresenter(bookPresenter);
        addPresenter(bookDetails);

        eventBus.register(this);
    }

    @Inject
    public void setAppController(AppController appController) {
        this.appController = appController;
    }    

    @Subscribe
    public void onOpenBook(OpenBook command) {
        appController.showPath(NavigationPathBuilder.create()
                .append(Resources.MODULE_BOOKS)
                .append(Resources.PRESENTER_BOOK_DETAILS)
                .build());        
        bookDetails.loadBookDetails(command.getBook());
    }

    @Subscribe
    public void onShowBooks(ShowBooks command) {
        appController.showPath(NavigationPathBuilder.create()
                .append(Resources.MODULE_BOOKS)
                .append(Resources.PRESENTER_BOOKS)
                .build());
        bookPresenter.loadBooks();
    }
}